import { HttpError, toSuccess, useEntityManager } from "@sker/core";
import { useData } from '@sker/runtime';
import { z } from "zod";

import { AutoUser } from "@sker/entities";
import { BaiYunCore } from "./core";

const appid = `wxaadbab9d13edff20`

/**
 * 420123485
    mgkj888888
 */

export default async () => {
    const input = z.object({
        token: z.string()
    })
    const body = input.parse(useData())
    return await useEntityManager(async m => {
        const user = await m.findOne(AutoUser, { where: { token: body.token } })
        if (!user) throw new HttpError(`参数错误`)
        const setting = user.setting;
        if (!setting) throw new HttpError(`数错误`)
        const baiyunSetting = setting.baiyun;
        const baiyun = new BaiYunCore(baiyunSetting.account, baiyunSetting.password)
        const res = await baiyun.apiRecord(appid)
        if (res) {
            return toSuccess(res)
        } else {
            throw new HttpError(`操作失败`)
        }
    })

}
